package com.jiuzhou.guanwang.jzcp.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.bean.ExpDetailResult;
import com.jiuzhou.guanwang.jzcp.netutil.NetRequestUtil;
import com.jiuzhou.guanwang.jzcp.utils.GlideCircleTransform;
import com.vise.log.ViseLog;

import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ExpDetailActivity extends AppCompatActivity {
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.tv_liansai)
    TextView tvLiansai;
    @BindView(R.id.tv_des)
    TextView tvDes;
    private String userName;
    private Context context;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView((int) R.layout.activity_expert_detail);
        context = this;
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
    }

    private void initData() {
        this.userName = getIntent().getStringExtra("userName");
        Map<String, String> map = new HashMap<>();
        map.put("cfrom", "ios");
        map.put("channel", "159cpgfbios");
        map.put("name", userName);
        map.put("version", "1");
        map.put("type", "1");
        Callback.Cancelable post = NetRequestUtil.getInstance().get("https://smapi.159cai.com/getFamousExplainList.php"
                , map, 101,
                new NetRequestUtil.NetResponseListener<String>() {
                    @Override
                    public void onSuccess(String response, int requestCode) {
                        ViseLog.d(response);
                        ExpDetailResult expDetailResult = (ExpDetailResult) new Gson().fromJson(response, ExpDetailResult.class);
                        tvName.setText(expDetailResult.getAuthName());
                        tvTag.setText(expDetailResult.getAuthTag());
                        if( expDetailResult.getAuthTag().contains("玩家")){
                            tvTag.setTextColor(getResources().getColor(R.color.red));
                            tvTag.setBackground(getResources().getDrawable(R.drawable.btn_with_border_red));
                        }else if(expDetailResult.getAuthTag().contains("彩票")){
                            tvTag.setTextColor(getResources().getColor(R.color.blue1));
                            tvTag.setBackground(getResources().getDrawable(R.drawable.btn_with_border_blue));
                        }else if(expDetailResult.getAuthTag().contains("TV")){
                            tvTag.setTextColor(getResources().getColor(R.color.orange));
                            tvTag.setBackground(getResources().getDrawable(R.drawable.btn_with_border_orange));
                        }else{
                            tvTag.setTextColor(getResources().getColor(R.color.purple));
                            tvTag.setBackground(getResources().getDrawable(R.drawable.btn_with_border_purple));
                        }

                        tvLiansai.setText(expDetailResult.getAuthadvantage());
                        tvDes.setText(expDetailResult.getAuthdescription());

                        if (!TextUtils.isEmpty(expDetailResult.getAuthheadImlUrl())) {
                            Glide.with(context).load(expDetailResult.getAuthheadImlUrl()).bitmapTransform(new GlideCircleTransform(context)).into(ivHead);
                            Glide.with(context).load(expDetailResult.getAuthheadImlUrl()).into(ivHead);
                        }else{
                            Glide.with(context).load("https://img.dongqiudi.com/soccer/data/logo/team/team_default.png").into(ivHead);
                        }
                    }

                    @Override
                    public void onFailed(String response, int requestCode) {
                        ViseLog.e("请求失败");
                        showToast("返回失败");
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("返回解析异常");
                    }

                    @Override
                    public void onFinished() {
                    }
                });
    }

    public void showToast(String text) {
        Toast.makeText(this, "" + text, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int resId) {
        Toast.makeText(this, this.getResources().getText(resId), Toast.LENGTH_SHORT).show();
    }

    public void back(View targv) {
        finish();
    }
}